package com.gscitysfy.cus.drain.modules.resource.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gscitysfy.cloudfrmwk.assistant.domain.Result;
import com.gscitysfy.cloudfrmwk.portal.feign.DictionaryService;
import com.gscitysfy.cloudfrmwk.portal.feign.DistrictService;
import com.gscitysfy.cloudfrmwk.portal.vo.DictionaryItemVO;
import com.gscitysfy.cus.drain.constants.DicConstant;
import com.gscitysfy.cus.drain.constants.RedisKeyConstant;
import com.gscitysfy.cus.drain.modules.resource.dto.DrainBaseChGoodsExcel;
import com.gscitysfy.cus.drain.modules.resource.dto.DrainBaseChGoodsRequest;
import com.gscitysfy.cus.drain.modules.resource.dto.DrainBaseChGoodsResponse;
import com.gscitysfy.cus.drain.modules.resource.dto.DrainBaseChGoodsStatistics;
import com.gscitysfy.cus.drain.modules.resource.entity.DrainBaseChGoods;
import com.gscitysfy.cus.drain.modules.resource.entity.DrainBaseMnWarehouse;
import com.gscitysfy.cus.drain.modules.resource.mapper.DrainBaseChGoodsMapper;
import com.gscitysfy.cus.drain.modules.resource.mapper.DrainBaseMnWarehouseMapper;
import com.gscitysfy.cus.drain.modules.resource.service.IDrainBaseChGoodsService;
import com.gscitysfy.cus.drain.utils.HttpResponseUtil;
import com.gscitysfy.cus.drain.utils.RedisUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 仓库物资子表 服务实现类
 * </p>
 *
 * @author user
 * @since 2022-10-24 16:58:09
 */
@Service
public class DrainBaseChGoodsServiceImpl extends ServiceImpl<DrainBaseChGoodsMapper, DrainBaseChGoods> implements IDrainBaseChGoodsService {

    @Resource
    private DrainBaseChGoodsMapper drainBaseChGoodsMapper;

    @Resource
    private DistrictService districtService;

    @Resource
    private DictionaryService dictionaryService;

    @Resource
    private DrainBaseMnWarehouseMapper baseMnWarehouseMapper;

    @Resource
    private RedisUtil redisUtil;


    @Override
    public Boolean delete(Long id) {
        DrainBaseChGoods drainBaseChGoods = drainBaseChGoodsMapper.selectById(id);
        drainBaseChGoods.setCmUsable(true);
        return this.saveOrUpdate(drainBaseChGoods);
    }

    @Override
    public IPage<DrainBaseChGoodsResponse> getByPage(Page<DrainBaseChGoodsRequest> page, DrainBaseChGoodsRequest request) {
        IPage<DrainBaseChGoodsResponse> pageResponse = drainBaseChGoodsMapper.selecByRequest(page, request);
        List<DrainBaseChGoodsResponse> records = pageResponse.getRecords();
        if (CollectionUtils.isNotEmpty(records)) {
            for (DrainBaseChGoodsResponse record : records) {
                Result<String> cmOrgflagresult = districtService.getDistrictNameByCode(record.getCmOrgflag());
                if (cmOrgflagresult.getStatus() == 200) {
                    record.setCmOrgflag(cmOrgflagresult.getData());
                }

            }
        }
        return pageResponse;
    }

    @Override
    public List<DrainBaseChGoodsStatistics> iconStatistics(DrainBaseChGoodsRequest request) {
        QueryWrapper<DrainBaseMnWarehouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cm_usable",false);
        List<DrainBaseChGoodsStatistics> list = new ArrayList<>();
        List<DrainBaseMnWarehouse> drainBaseMnWarehouses = baseMnWarehouseMapper.selectList(queryWrapper);
        for (DrainBaseMnWarehouse drainBaseMnWarehouse : drainBaseMnWarehouses) {
            request.setWarehouseId(drainBaseMnWarehouse.getId());
            DrainBaseChGoodsStatistics statistics = drainBaseChGoodsMapper.iconStatistics(request);
            statistics.setCmKid(drainBaseMnWarehouse.getCmKid());
            statistics.setWarehouseName(drainBaseMnWarehouse.getWarehouseName());
            list.add(statistics);
        }
        if (CollectionUtils.isNotEmpty(list)){
            list = list.stream().sorted(Comparator.comparing(DrainBaseChGoodsStatistics::getTotal)).collect(Collectors.toList());
        }

        return list;
    }

    @Override
    public void export(HttpServletResponse response, List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        List<DrainBaseChGoodsResponse> records = drainBaseChGoodsMapper.selecByIds(ids);
        if (CollectionUtils.isNotEmpty(records)) {
            for (DrainBaseChGoodsResponse record : records) {
                Result<String> cmOrgflagresult = districtService.getDistrictNameByCode(record.getCmOrgflag());
                if (cmOrgflagresult.getStatus() == 200) {
                    record.setCmOrgflag(cmOrgflagresult.getData());
                }
                Map<Object, Object> problemTypeDic = redisUtil.hmget(RedisKeyConstant.DATA_DICT_KEY + DicConstant.YAYJWZLX_TYPE);
                if (problemTypeDic!=null && problemTypeDic.size()>0) {
                    if (StringUtils.isNotEmpty(record.getGoodsType())){
                        if (problemTypeDic.get(record.getGoodsType())!=null){
                            record.setGoodsType(problemTypeDic.get(record.getGoodsType()).toString());
                        }
                    }

                }
            }
            String fileName = "物资信息.xlsx";
            try {
                HttpResponseUtil.setResponseHeaders(response, fileName);
                EasyExcel.write(response.getOutputStream(), DrainBaseChGoodsExcel.class)
                        .sheet("物资信息")
                        .doWrite(() -> records);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}
